package com.ternnetwork.clustersyn.listener;

import java.io.File;
import org.apache.commons.io.monitor.FileAlterationListener;
import org.apache.commons.io.monitor.FileAlterationObserver;
import org.apache.ftpserver.command.impl.RemoteOperateFileNameCache;
import org.apache.log4j.Logger;

import com.ternnetwork.clustersyn.util.ClusterUtil;

public class ClusterFileAlterationListener implements FileAlterationListener {

	private static final Logger log = Logger.getLogger(ClusterFileAlterationListener.class);
	
	@Override
	public void onDirectoryChange(File file) {
		// TODO Auto-generated method stub
		log.info("目录改变到"+file);
	}

	@Override
	public void onDirectoryCreate(File file) {
		
	
		
		log.info("创建目录"+file);
	}

	@Override
	public void onDirectoryDelete(File file) {
		// TODO Auto-generated method stub
		log.info("删除目录"+file);
	}

	@Override
	public void onFileChange(File file) {
		if(file.getName().equals(".DS_Store")){
			return;
		}
		log.info("文件改变 "+file);
	    String path=file.getPath().replaceAll("\\\\","/");
	    String homeDirectory=RemoteOperateFileNameCache.getRemoteUploadFileFtpHomeDirectory(path);
		if(homeDirectory!=null&&!"".equals(homeDirectory)){
			log.info(path+"  由其它实例改变,本实例不进行同步分发。 ");
			RemoteOperateFileNameCache.setRemoteUploadFileFtpHomeDirectory(path,"");
		}else{
			log.info(path+"  由当前实改变,将进行同步分发。 ");
			try {
				ClusterUtil.saveToCluster(path);
			} catch (Exception e) {
				e.printStackTrace();
			} 
		}
		
	}

	@Override
	public void onFileCreate(File file) {
		if(file.getName().equals(".DS_Store")){
			return;
		}
		log.info("文件名："+file.getName());
		log.info("文件创建 "+file);
		String path=file.getPath().replaceAll("\\\\","/");
		 String homeDirectory=RemoteOperateFileNameCache.getRemoteUploadFileFtpHomeDirectory(path);
	    if(homeDirectory!=null&&!"".equals(homeDirectory)){
	    	log.info(path+"  由其它实例创建,不进行同步分发。 ");
			RemoteOperateFileNameCache.setRemoteUploadFileFtpHomeDirectory(path,"");
		}else{
			log.info(path+"  由当前实例创建,将进行同步分发。 ");
			try {
				ClusterUtil.saveToCluster(path);
			} catch (Exception e) {
				e.printStackTrace();
			} 
		}
		
	    
	}

	@Override
	public void onFileDelete(File file) {
		if(file.getName().equals(".DS_Store")){
			return;
		}
		log.info("文件删除 "+file);
 	    String path=file.getPath().replaceAll("\\\\","/");
 	    
    	if(RemoteOperateFileNameCache.getRemoteDeleteFileFtpHomeDirectory(path)!=null){
    		log.info(path+"  由其它实例删除,不进行同步删除。 ");
			RemoteOperateFileNameCache.setRemoteDeleteFileFtpHomeDirectory(path,"");
		}else{
			log.info(path+"  由当前实例删除,将进行同步删除。 ");
			try {
				ClusterUtil.deleteFromCluster(path);
			} catch (Exception e) {
				e.printStackTrace();
			} 
		}
		
		
	}

	@Override
	public void onStart(FileAlterationObserver observer) {
		// TODO Auto-generated method stub
		//System.out.println("onStart ");
	}

	@Override
	public void onStop(FileAlterationObserver observer) {
		// TODO Auto-generated method stub
		//System.out.println("onStop ");
	}

}
